package com.gitee.wsl.android.drawable

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.ColorFilter
import android.graphics.DashPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.graphics.PixelFormat
import android.graphics.RectF
import android.graphics.drawable.Drawable

/***
 * 名称：GridDrawable<br></br>
 * 描述：自定义米格背景视图
 * 最近修改时间：
 * @since 2017/11/16
 * @author king
 */
class GridDrawable(
    private val mWidth: Float,
    private val mHeight: Float,
    private val backgroundColor: Int
) : Drawable() {
    private var mPaint: Paint? = null
    private var mDashPaint: Paint? = null
    private var mLinePaint: Paint? = null
    private var mBitmap: Bitmap? = null
    private var mCanvas: Canvas? = null
    private var linePath: Path? = null
    private var dashPath: Path? = null

    init {
        init()
    }

    fun init() {
        //边框画笔
        mPaint = Paint()
        mPaint!!.style = Paint.Style.STROKE
        mPaint!!.isAntiAlias = true
        mPaint!!.strokeWidth = 10.0f
        mPaint!!.color = Color.parseColor("#c4c4c4")

        //虚线画笔
        mDashPaint = Paint()
        mDashPaint!!.style = Paint.Style.STROKE
        mDashPaint!!.isAntiAlias = true
        mDashPaint!!.strokeWidth = 5.0f
        mDashPaint!!.color = Color.parseColor("#c4c4c4")
        mDashPaint!!.pathEffect = DashPathEffect(floatArrayOf(50f, 40f), 0f)
        mLinePaint = Paint()
        mLinePaint!!.style = Paint.Style.STROKE
        mLinePaint!!.isAntiAlias = true
        mLinePaint!!.strokeWidth = 5.0f
        mLinePaint!!.color = Color.parseColor("#c4c4c4")
        linePath = Path()
        dashPath = Path()
        mBitmap = Bitmap.createBitmap(mWidth.toInt(), mHeight.toInt(), Bitmap.Config.ARGB_4444)
        mCanvas = Canvas(mBitmap!!)
        mCanvas!!.drawColor(backgroundColor)
        doDraw()
    }

    private fun doDraw() {
        val rect = RectF(0f, 0f, mWidth, mHeight)
        mCanvas!!.drawRect(rect, mPaint!!)
        linePath!!.moveTo(0f, (mHeight / 2))
        linePath!!.lineTo(mWidth, (mHeight / 2))
        mCanvas!!.drawPath(linePath!!, mLinePaint!!)
        linePath!!.moveTo((mWidth / 2), 0f)
        linePath!!.lineTo((mWidth / 2), mHeight)
        mCanvas!!.drawPath(linePath!!, mLinePaint!!)
        dashPath!!.moveTo(0f, 0f)
        dashPath!!.lineTo(mWidth, mHeight)
        mCanvas!!.drawPath(dashPath!!, mDashPaint!!)
        dashPath!!.moveTo(mWidth, 0f)
        dashPath!!.lineTo(0f, mHeight)
        mCanvas!!.drawPath(dashPath!!, mDashPaint!!)
    }

    override fun draw(canvas: Canvas) {
        canvas.drawBitmap(mBitmap!!, 0f, 0f, mPaint)
    }

    override fun setAlpha(i: Int) {
        mPaint!!.alpha = i
        mDashPaint!!.alpha = i
    }

    override fun setColorFilter(colorFilter: ColorFilter?) {
        mPaint!!.colorFilter = colorFilter
        mDashPaint!!.colorFilter = colorFilter
    }


    @Deprecated("Deprecated in Java",
        ReplaceWith("PixelFormat.TRANSLUCENT", "android.graphics.PixelFormat")
    )
    override fun getOpacity(): Int {
        return PixelFormat.TRANSLUCENT
    }
}